#include "PhysicsGame.h"
#include "HavokWorld.h"
#include "Domino.h"

PhysicsGame* ThePhysicsGame::physicsGame = NULL;

PhysicsGame::PhysicsGame(void)
{
	level = NULL;
	player = NULL;
}

PhysicsGame::~PhysicsGame(void)
{

}

void PhysicsGame::Init()
{
	TheGameWorld.Init();

	HavokWorld::Get().Init();

	// lock the havok world
	HavokWorld::Get().Lock();

	player = new DominoPlayer(6);
	player->Position.x = 90;
	player->Position.y = 50;
	player->Position.z = -90;
	player->AddObjectToSimulation(HavokWorld::Get().physicsWorld);
	TheGameWorld.Add(player);

	level = new Level(240,7,280);
	InitLevel();
	level->AddObjectToSimulation(HavokWorld::Get().physicsWorld);
	TheGameWorld.Add(level);

	domino = new Domino();
	domino->Init(D3DXVECTOR3(90,14,-60));
	TheGameWorld.Add(domino);
	
	TheGameWorld.Cameras.AddCamera(0, new Camera(0, 110,-330.0f ));
	TheGameWorld.Cameras.SetCurrentCamera(0);
	TheGameWorld.Lights.AddDirectionalLight(new DirectionalLight(D3DXVECTOR3(-0.5f, -1.0f, -0.1f)));
	TheGameWorld.Lights.SetAmbientColour(0.1,0.1,0.1);
	TheGameWorld.Lights.AddDirectionalLight(new DirectionalLight(D3DXVECTOR3(0.5f, -1.0f, -0.1f)));
	TheGameWorld.Lights.SetAmbientColour(0.1,0.1,0.1);
	TheGameWorld.Lights.AddDirectionalLight(new DirectionalLight(D3DXVECTOR3(-0.5f, -1.0f, 0.3f)));
	TheGameWorld.Lights.SetAmbientColour(0.1,0.1,0.1);
	TheGameWorld.Lights.AddDirectionalLight(new DirectionalLight(D3DXVECTOR3(0.5f, -1.0f, 0.3f)));
	TheGameWorld.Lights.SetAmbientColour(0.1,0.1,0.1);

	HavokWorld::Get().StartVisualDebugger();

	// lock the havok world
	HavokWorld::Get().Unlock();
}

void PhysicsGame::Update()
{
	// update physics
	HavokWorld::Get().Update();

	// lock the world
	HavokWorld::Get().Lock();

	//update game
	Game::Update();
	if (player->Position.y < 0 || player->Position.y>100)
	{
		player->RemoveObjectFromSimulation(HavokWorld::Get().physicsWorld);
		player = NULL;
		player = new DominoPlayer(6);
		player->Position.x = 90;
		player->Position.y = 30;
		player->Position.z = -90;
		player->AddObjectToSimulation(HavokWorld::Get().physicsWorld);
		TheGameWorld.Add(player);
	}

	HavokWorld::Get().Unlock();
}

void PhysicsGame::Render()
{
	Game::Render();

	//MyString Score = "Score: ";
	////Score += ThePhysicsGame::Get().Player->Score;

	//SimpleRender::Get().DrawText(10,10,Score.Get());
}

void PhysicsGame::InitLevel()
{
	int TileNumX = 19;
	int TileNumY = 22;
	const int Level_Size = TileNumX * TileNumY;

	float unitWidth = level->dimentions.x / TileNumX;
	float unitHeight = level->dimentions.z / TileNumY;


	int tiles[] = {
		1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};


		int row = 0;
		int column = 0;
		for(unsigned int i=0; i<Level_Size; i++)
		{
			//translate between tile map and actual area on floor
			float TilePosX = (column*unitWidth) + -(level->dimentions.x/2) + (unitWidth/2);
			float TilePosY = -(row*unitHeight) + (level->dimentions.z/2) - (unitHeight/2);

			if(tiles[i]==1)
			{
				level->CreateWall(TilePosX, TilePosY, unitWidth, unitHeight);
			}

			// move on..
			column++;
			if(column == TileNumX)
			{
				column = 0;
				row++;
			}
		}
}
